package i.i.myapplication;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;


//import i.i.myapplication.model.Music;
import i.i.myapplication.controller.MusicAdapter;
import i.i.myapplication.controller.VideoAdapter;
import i.i.myapplication.model.Music;
import i.i.myapplication.model.Video;
import i.i.myapplication.view.MusicDetailActivity;
import i.i.myapplication.view.MusicService;
import i.i.myapplication.view.VideoDetailActivity;


//package i.i.myapplication;


public class VideoFragment extends Fragment {

    private ListView listView;
    private VideoAdapter adapter; // 修改这里的类型

    private List<Music> musicList;
    private List<Video> videoList;
    private MusicService.MusicControl musicControl;  // 绑定Service后拿到的控制对象
    private ServiceConnection serviceConnection;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_video, container, false);
        listView = rootView.findViewById(R.id.lv);
        videoList = new ArrayList<>();
//        videoList.add(new Video(R.drawable.img_01, "Big Buck Bunny", "动画演示", "https://storage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"));
//        videoList.add(new Video(R.drawable.img_02, "Sintel", "开源短片", "https://storage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"));


        videoList.add(new Video(R.drawable.img_01, "本地视频1", "动画演示", R.raw.es));
        videoList.add(new Video(R.drawable.img_02, "本地视频2", "开源短片", R.raw.kelake));


//        videoAdapter = new VideoAdapter(requireContext(), videoList, position -> {
//            Video video = videoList.get(position);
//            Intent intent = new Intent(getContext(), VideoDetailActivity.class);
//            intent.putExtra("video_url", video.getVideoUrl());
//            startActivity(intent);
//        });

//        listView.setAdapter(videoAdapter);

        initMusicList();
        adapter = new VideoAdapter(requireContext(), videoList); // 不传 position -> {} 回调
        listView.setAdapter(adapter);



        listView.setAdapter(adapter);


        listView.setOnItemClickListener((parent, view, position, id) -> {
            Video video = videoList.get(position);
            Intent intent = new Intent(requireContext(), VideoDetailActivity.class);
//            intent.putExtra("video_url", video.getVideoUrl());
            intent.putExtra("video_res_id", video.getVideoResId());

            startActivity(intent);
        });







        // 绑定MusicService，获取控制对象
        Intent intent = new Intent(requireContext(), MusicService.class);
        requireActivity().bindService(intent, getServiceConnection(), Context.BIND_AUTO_CREATE);

        return rootView;
    }

    private void initMusicList() {
        musicList = new ArrayList<>();

        musicList.add(new Music(R.drawable.img_01, "Big Buck Bunny", "导演:Sacha Goedegebure", R.raw.music1));
        musicList.add(new Music(R.drawable.img_02, "Love Story", "Taylor Swift", R.raw.music2));

        // 根据你资源添加更多...
    }

    private ServiceConnection getServiceConnection() {
        if (serviceConnection == null) {
            serviceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    musicControl = (MusicService.MusicControl) service;
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    musicControl = null;
                }
            };
        }
        return serviceConnection;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (serviceConnection != null) {
            requireActivity().unbindService(serviceConnection);
            serviceConnection = null;
        }
    }
}
